package com.lbssoft.gpsbd.aspect;

import com.alibaba.fastjson.JSON;
import com.lbssoft.datalink.EntityManager;
import com.lbssoft.datalink.entity.SyncGeofence;
import com.lbssoft.datalink.util.Log;
import com.lbssoft.gpsbd.common.Constant;
import com.lbssoft.gpsbd.common.ErrorEnum;
import com.lbssoft.gpsbd.common.Utils;
import com.lbssoft.gpsbd.dao.GeofenceMapper;
import com.lbssoft.gpsbd.datalink.Context;
import com.lbssoft.gpsbd.model.Geofence;
import com.lbssoft.gpsbd.model.Result;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Map;


@Aspect
@Component
public class SyncGeofenceAspect {
    //使用org.slf4j.Logger,这是Spring实现日志的方法
    private final static Logger logger = LoggerFactory.getLogger(SyncGeofenceAspect.class);

    @Autowired
    GeofenceMapper geofenceMapper;

    /**
     * 定义AOP扫描路径
     * 第一个注解只扫描aopTest方法
     */
    @Pointcut("execution(public * com.lbssoft.gpsbd.controller.GeofenceController.add(..)) || " +
            "execution(public * com.lbssoft.gpsbd.controller.GeofenceController.delete(..)) || " +
            "execution(public * com.lbssoft.gpsbd.controller.GeofenceController.update(..))")
    public void geofence(){}

    /**
     * 记录HTTP请求开始时的日志
     */
    @Before("geofence()")
    public void doBefore(JoinPoint joinPoint){
    }

    /**
     * 记录HTTP请求结束时的日志
     */
    @After("geofence()")
    public void doAfter(){
    }

    /**
     * 获取返回内容
     * @param object
     */
    @AfterReturning(returning = "object",pointcut = "geofence()")
    public void doAfterReturn(Object object){
    }

    /**
     * 环绕通知（Around advice） ：包围一个连接点的通知，类似Web中Servlet规范中的Filter的doFilter方法。可以在方法的调用前后完成自定义的行为，也可以选择不执行。
     * @param joinPoint
     */
    @Around("geofence()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable
    {
        com.lbssoft.datalink.model.Result res = new com.lbssoft.datalink.model.Result();
        Geofence geofence;
        Geofence localGeofence;
        SyncGeofence syncGeofence = new SyncGeofence();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Object[] args = joinPoint.getArgs();
        Method method = signature.getMethod(); //获取被拦截的方法
        String methodName = method.getName(); //获取被拦截的方法名
        //判断是否开启同步
        if (!Constant.DEV_MODE) {
            System.out.println("====同步开始=====");
            switch (methodName) {
                //添加围栏
                case "add":
                    geofence = (Geofence) args[0];
                    if(!Utils.geofenceDataCheck(geofence)){
                        return new Result(ErrorEnum.E_002.getErrorCode(), ErrorEnum.E_002.getErrorMsg(), "");
                    }

                    syncGeofence.setName(geofence.getName());
                    syncGeofence.setType(geofence.getType());
                    syncGeofence.setArea(geofence.getArea());

                    res = Context.entityManager.addGeoFence(syncGeofence);
                    if (res.getCode() != 0) {
                        return new Result(ErrorEnum.E_005.getErrorCode(), ErrorEnum.E_005.getErrorMsg(), "");
                    }
                    geofence.setUuid(((Map)res.getData()).get("uuid").toString());
                    break;
                case "delete":
                    geofence = geofenceMapper.getInfo(Long.parseLong(String.valueOf(args[0])));
                    res = Context.entityManager.delGeoFence(geofence.getUuid());
                    if (res.getCode() != 0) {
                        return new Result(ErrorEnum.E_005.getErrorCode(), ErrorEnum.E_005.getErrorMsg(), "");
                    }
                    break;
                //修改围栏
                case "update":
                    geofence = (Geofence) args[1];
                    if(!Utils.geofenceDataCheck(geofence)){
                        return new Result(ErrorEnum.E_002.getErrorCode(), ErrorEnum.E_002.getErrorMsg(), "");
                    }
                    localGeofence = geofenceMapper.getInfo(Long.parseLong(String.valueOf(args[0])));
                    syncGeofence.setUuid(localGeofence.getUuid());
                    syncGeofence.setName(geofence.getName());
                    syncGeofence.setType(geofence.getType());
                    syncGeofence.setArea(geofence.getArea());
                    res = Context.entityManager.updateGeoFence(syncGeofence);
                    if (res.getCode() != 0) {
                        return new Result(ErrorEnum.E_005.getErrorCode(), ErrorEnum.E_005.getErrorMsg(), "");
                    }
                    break;
                default:

            }
            System.out.println("=====同步结束=====");
        }


        return  joinPoint.proceed();
    }

}